using System.Configuration;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Routing;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.template;

namespace Umbraco.Tests.Routing
{
	[TestFixture]
	public class LookupByNiceUrlTests : BaseRoutingTest
	{
        public override void Initialize()
        {
            base.Initialize();            
        }

        public override void TearDown()
        {
            base.TearDown();
        }

		/// <summary>
		/// We don't need a db for this test, will run faster without one
		/// </summary>
		protected override bool RequiresDbSetup
		{
			get { return false; }
		}

        
		[TestCase("/", 1046)]
		[TestCase("/default.aspx", 1046)] //this one is actually rather important since this is the path that comes through when we are running in pre-IIS 7 for the root document '/' !
		[TestCase("/Sub1", 1173)]
		[TestCase("/sub1", 1173)]
		[TestCase("/sub1.aspx", 1173)]
		[TestCase("/home/sub1", -1)] // should fail
		
		// these two are special. getNiceUrl(1046) returns "/" but getNiceUrl(1172) cannot also return "/" so
		// we've made it return "/test-page" => we have to support that url back in the lookup...
		[TestCase("/home", 1046)]
		[TestCase("/test-page", 1172)]
		public void Match_Document_By_Url_Hide_Top_Level(string urlString, int expectedId)
		{
			var routingContext = GetRoutingContext(urlString);
			var url = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url
			var docreq = new PublishedContentRequest(url, routingContext);
			var lookup = new LookupByNiceUrl(false);
            SettingsForTests.HideTopLevelNodeFromPath = true;

			var result = lookup.TrySetDocument(docreq);

			if (expectedId > 0)
			{
				Assert.IsTrue(result);
				Assert.AreEqual(expectedId, docreq.DocumentId);
			}
			else
			{
				Assert.IsFalse(result);
			}
		}

        
		[TestCase("/", 1046)]
		[TestCase("/default.aspx", 1046)] //this one is actually rather important since this is the path that comes through when we are running in pre-IIS 7 for the root document '/' !
		[TestCase("/home", 1046)]
		[TestCase("/home/Sub1", 1173)]
		[TestCase("/Home/Sub1", 1173)] //different cases
		[TestCase("/home/Sub1.aspx", 1173)]
		public void Match_Document_By_Url(string urlString, int expectedId)
		{
			var routingContext = GetRoutingContext(urlString);
			var url = routingContext.UmbracoContext.CleanedUmbracoUrl;	//very important to use the cleaned up umbraco url		
			var docreq = new PublishedContentRequest(url, routingContext);			
			var lookup = new LookupByNiceUrl(false);
            SettingsForTests.HideTopLevelNodeFromPath = false;
			
			var result = lookup.TrySetDocument(docreq);

			Assert.IsTrue(result);
			Assert.AreEqual(expectedId, docreq.DocumentId);
		}

	}
}